﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySpace.Data;
public partial class Manage_New_Type : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBd();
        }
    }
    protected void DataBd()
    {
        DBHandler dbh = GetHandler.Init();
        dbh.CommandText = "select * from News_big_type order by bigclassid desc";
        Repeater1.DataSource = dbh.ExecuteDataTable();
        Repeater1.DataBind();
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater rp = (Repeater)e.Item.FindControl("Repeater2");
            Label lb = (Label)e.Item.FindControl("bigid");
            int sid = Convert.ToInt32(lb.Text.ToString());
            DBHandler dbh = GetHandler.Init();
            dbh.CommandText = string.Format("select * from News_small_type where forbigclass={0} order by smallclassid desc", sid);
            rp.DataSource = dbh.ExecuteDataTable();
            rp.DataBind();
        }
    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "addsmall")
        {
            //添加小类
            big.Visible = false;
            small.Visible = true;
            Hiddenid.Value = ((Label)e.Item.FindControl("bigid")).Text;
        }
        if (e.CommandName == "udatebigclass")
        {
            DBHandler dbh = GetHandler.Init();
            //dbh.CommandText = "update News_big_type set bigclassname=? and bigclassurl=? where bigclassid=?";
            TextBox name = (TextBox)e.Item.FindControl("bigname");
            TextBox url = (TextBox)e.Item.FindControl("bigurl");
            Label bigid = (Label)e.Item.FindControl("bigid");
            dbh.CommandText = string.Format("update [News_big_type] set bigclassname='{0}',bigclassurl='{1}' where bigclassid={2}", name.Text, url.Text, Convert.ToInt32(bigid.Text));
            try
            {
                dbh.ExecuteNonQuery();
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('大类更新成功');</script>");
            }
            catch
            {
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('大类更新失败');</script>");
            }
            DataBd();
        }
        if (e.CommandName == "addbigclass")
        {
            //添加大类
            big.Visible = true;
            small.Visible = false;
        }
        if (e.CommandName == "delbigclass")
        {
            //删除大类
            DBHandler dbh = GetHandler.Init();
            dbh.CommandText = string.Format("delete from News_big_type where bigclassid={0}", Convert.ToInt32(((Label)e.Item.FindControl("bigid")).Text));
            try
            {

                dbh.ExecuteNonQuery();
                dbh.CommandText = string.Format("delete from News_small_type where forbigclass={0}", Convert.ToInt32(((Label)e.Item.FindControl("bigid")).Text));
                dbh.ExecuteNonQuery();
                dbh.CommandText = string.Format("delete from news where BigClassId={0}", Convert.ToInt32(((Label)e.Item.FindControl("bigid")).Text));
                dbh.ExecuteNonQuery();
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('大类删除成功');</script>");
                DataBd();
            }
            catch
            {
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('大类删除失败');</script>");
                DataBd();
            }
        }
    }
    protected void Repeater2_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "updatesmall")
        {
            //更新小类
            //Repeater Repeater2 = (Repeater)(((LinkButton)e.CommandSource).Parent.Parent);
            DBHandler dbh = GetHandler.Init();
            TextBox name = (TextBox)e.Item.FindControl("smallname");
            TextBox url = (TextBox)e.Item.FindControl("smallurl");
            HiddenField bigid = (HiddenField)e.Item.FindControl("smallid");
            dbh.CommandText = string.Format("update News_small_type set smallclassname='{0}',smallclassurl='{1}' where smallclassid={2}", name.Text, url.Text, Convert.ToInt32(bigid.Value));
            try
            {
                dbh.ExecuteNonQuery();
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('小类更新成功');</script>");
            }
            catch
            {
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('小类更新失败');</script>");
            }
            DataBd();

        }
        if (e.CommandName == "delsmall")
        {
            //删除小类
            DBHandler dbh = GetHandler.Init();
            dbh.CommandText = string.Format("delete from News_small_type where smallclassid={0}", Convert.ToInt32(((HiddenField)e.Item.FindControl("smallid")).Value));
            try
            {

                dbh.ExecuteNonQuery();
                dbh.CommandText = string.Format("delete from news where SmallClassId={0}", Convert.ToInt32(((HiddenField)e.Item.FindControl("smallid")).Value));
                dbh.ExecuteNonQuery();
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('小类删除成功');</script>");
                DataBd();
            }
            catch
            {
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('小类删除失败');</script>");
                DataBd();
            }
        }
    }
    protected void buttonbigadd_Click(object sender, EventArgs e)
    {
        DBHandler dbh = GetHandler.Init();
        dbh.CommandText = "Insert Into News_big_type(bigclassname,bigclassurl) Values(?,?)";
        dbh.ClearParameter();
        dbh.AddParamerer("bigclassname", this.addbigclassname.Text.Trim());
        dbh.AddParamerer("bigclassurl", this.addbigclassurl.Text.Trim());
        try
        {
            dbh.ExecuteNonQuery();
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('大类添加成功');</script>");
            big.Visible = false;
            DataBd();
        }
        catch
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('大类添加失败');</script>");
        }
    }
    protected void buttonsmalladd_Click(object sender, EventArgs e)
    {
        DBHandler dbh = GetHandler.Init();
        dbh.CommandText = "Insert Into News_small_type(smallclassname,smallclassurl,forbigclass) Values(?,?,?)";
        dbh.ClearParameter();
        dbh.AddParamerer("bigclassname", this.addsmallclassname.Text.Trim());
        dbh.AddParamerer("bigclassurl", this.addsmallclassurl.Text.Trim());
        dbh.AddParamerer("forbigclass", Convert.ToInt32(Hiddenid.Value));
        try
        {
            dbh.ExecuteNonQuery();
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('小类添加成功');</script>");
            small.Visible = false;
            DataBd();
        }
        catch
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('小类添加失败');</script>");

        }
    }
    protected void cancel_Click(object sender, EventArgs e)
    {
        big.Visible = false;
        small.Visible = false;
    }
    protected void badd_Click(object sender, EventArgs e)
    {
        //添加大类
        big.Visible = true;
        small.Visible = false;
    }
}